import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import ThreadedSshClient 1.0
import QtCore

Item {
    // color: "#26d198"
    ThreadedSshClient {
        id: sshClient
        onGetBackedEnvTypeFinished: function(ret, type_orr_error) {
            if (ret) {
                backendTypeLabel.text = type_orr_error
            }
        }

        onSetBackedEnvTypeFinished: function(ret, error) {

        }
    }

    Settings {
        id: sshSettings
    }

    ColumnLayout {
        anchors.centerIn: parent
        spacing: 10

        RowLayout { // 连接设置
            Rectangle { // 连接状态指示器
                // anchors.right: parent.right
                width: 12
                height: 12
                radius: 6
                color: sshClient.connectedState ? "green" : "red"
            }

            Label {
                text: "Host:"
            }
            TextField {
                id: hostField
                text: sshSettings.value("ssh/host", "").toString()
                Layout.preferredWidth: 150
                onTextChanged: sshSettings.setValue("ssh/host", text)
            }
            Label {
                text: "User:"
            }
            TextField {
                id: userField
                text: sshSettings.value("ssh/user", "").toString()
                Layout.preferredWidth: 150
                onTextChanged: sshSettings.setValue("ssh/user", text)
            }
            Label {
                text: "Password:"
            }
            TextField {
                id: passwordField
                text: sshSettings.value("ssh/password", "").toString()
                Layout.preferredWidth: 150
                onTextChanged: sshSettings.setValue("ssh/password", text)
            }
            Button {
                text: sshClient.connectedState ? "Disconnect" : "Connect"
                onClicked: {
                    if (sshClient.connectedState) {
                        sshClient.disconnectFromHost()
                    } else {
                        sshClient.connectToHost(hostField.text, 22, userField.text, passwordField.text)
                    }
                }
            }
        }

        ColumnLayout {
            enabled : sshClient.connectedState
            GroupBox {
                title: "Backend Type"
                Layout.fillWidth: true
                Layout.preferredHeight: 100

                RowLayout {
                    Label {
                        text: "Backend Type:"
                    }
                    Label {
                        id: backendTypeLabel
                        Layout.preferredWidth: 100
                    }

                    Button {
                        text: "Query"
                        onClicked: {
                            sshClient.getBackedEnvType()
                        }
                    }
                    Item {
                        Layout.preferredWidth: 100
                    }
                    ComboBox {
                        id: backendTypeComboBox
                        model: ["dev", "demo", "prod"]
                    }
                    Button {
                        text: "Set"
                        onClicked: {
                            sshClient.setBackedEnvType(backendTypeComboBox.currentText)
                        }
                    }
                }
            }
        }
    }

    // Button {
    // anchors.centerIn: parent
    // text: "Update Install"
    // onClicked: {
    // sshClient.updateInstall("C:/Users/Alex/Desktop/temp_log/install.tar.gz")
    // }
    // }

    Component.onCompleted: {
        // sshClient.connectToHost("192.168.110.31", 22, "root", "root")
    }
}